home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2009 February / PCWFEB09.iso / Software / Linux / Kubuntu 8.10 / kubuntu-8.10-desktop-i386.iso / casper / filesystem.squashfs / usr / lib / python2.5 / wsgiref / headers.pyc (.txt) < prev    next >
Python Compiled Bytecode  |  2008-10-29  |  8KB  |  187 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.5)
  3.  
  4. '''Manage HTTP Response Headers
  5.  
  6. Much of this module is red-handedly pilfered from email.Message in the stdlib,
  7. so portions are Copyright (C) 2001,2002 Python Software Foundation, and were
  8. written by Barry Warsaw.
  9. '''
  10. from types import ListType, TupleType
  11. import re
  12. tspecials = re.compile('[ \\(\\)<>@,;:\\\\"/\\[\\]\\?=]')
  13.  
  14. def _formatparam(param, value = None, quote = 1):
  15.     '''Convenience function to format and return a key=value pair.
  16.  
  17.     This will quote the value if needed or if quote is true.
  18.     '''
  19.     if value is not None and len(value) > 0:
  20.         if quote or tspecials.search(value):
  21.             value = value.replace('\\', '\\\\').replace('"', '\\"')
  22.             return '%s="%s"' % (param, value)
  23.         else:
  24.             return '%s=%s' % (param, value)
  25.     else:
  26.         return param
  27.  
  28.  
  29. class Headers:
  30.     '''Manage a collection of HTTP response headers'''
  31.     
  32.     def __init__(self, headers):
  33.         if type(headers) is not ListType:
  34.             raise TypeError('Headers must be a list of name/value tuples')
  35.         
  36.         self._headers = headers
  37.  
  38.     
  39.     def __len__(self):
  40.         '''Return the total number of headers, including duplicates.'''
  41.         return len(self._headers)
  42.  
  43.     
  44.     def __setitem__(self, name, val):
  45.         '''Set the value of a header.'''
  46.         del self[name]
  47.         self._headers.append((name, val))
  48.  
  49.     
  50.     def __delitem__(self, name):
  51.         '''Delete all occurrences of a header, if present.
  52.  
  53.         Does *not* raise an exception if the header is missing.
  54.         '''
  55.         name = name.lower()
  56.         self._headers[:] = _[1]
  57.  
  58.     
  59.     def __getitem__(self, name):
  60.         """Get the first header value for 'name'
  61.  
  62.         Return None if the header is missing instead of raising an exception.
  63.  
  64.         Note that if the header appeared multiple times, the first exactly which
  65.         occurrance gets returned is undefined.  Use getall() to get all
  66.         the values matching a header field name.
  67.         """
  68.         return self.get(name)
  69.  
  70.     
  71.     def has_key(self, name):
  72.         '''Return true if the message contains the header.'''
  73.         return self.get(name) is not None
  74.  
  75.     __contains__ = has_key
  76.     
  77.     def get_all(self, name):
  78.         '''Return a list of all the values for the named field.
  79.  
  80.         These will be sorted in the order they appeared in the original header
  81.         list or were added to this instance, and may contain duplicates.  Any
  82.         fields deleted and re-inserted are always appended to the header list.
  83.         If no fields exist with the given name, returns an empty list.
  84.         '''
  85.         name = name.lower()
  86.         return _[1]
  87.  
  88.     
  89.     def get(self, name, default = None):
  90.         """Get the first header value for 'name', or return 'default'"""
  91.         name = name.lower()
  92.         for k, v in self._headers:
  93.             if k.lower() == name:
  94.                 return v
  95.                 continue
  96.         
  97.         return default
  98.  
  99.     
  100.     def keys(self):
  101.         '''Return a list of all the header field names.
  102.  
  103.         These will be sorted in the order they appeared in the original header
  104.         list, or were added to this instance, and may contain duplicates.
  105.         Any fields deleted and re-inserted are always appended to the header
  106.         list.
  107.         '''
  108.         return [ k for k, v in self._headers ]
  109.  
  110.     
  111.     def values(self):
  112.         '''Return a list of all header values.
  113.  
  114.         These will be sorted in the order they appeared in the original header
  115.         list, or were added to this instance, and may contain duplicates.
  116.         Any fields deleted and re-inserted are always appended to the header
  117.         list.
  118.         '''
  119.         return [ v for k, v in self._headers ]
  120.  
  121.     
  122.     def items(self):
  123.         '''Get all the header fields and values.
  124.  
  125.         These will be sorted in the order they were in the original header
  126.         list, or were added to this instance, and may contain duplicates.
  127.         Any fields deleted and re-inserted are always appended to the header
  128.         list.
  129.         '''
  130.         return self._headers[:]
  131.  
  132.     
  133.     def __repr__(self):
  134.         return 'Headers(%s)' % `self._headers`
  135.  
  136.     
  137.     def __str__(self):
  138.         '''str() returns the formatted headers, complete with end line,
  139.         suitable for direct HTTP transmission.'''
  140.         return []([ '%s: %s' % kv for kv in self._headers ] + [
  141.             '',
  142.             ''])
  143.  
  144.     
  145.     def setdefault(self, name, value):
  146.         """Return first matching header value for 'name', or 'value'
  147.  
  148.         If there is no header named 'name', add a new header with name 'name'
  149.         and value 'value'."""
  150.         result = self.get(name)
  151.         if result is None:
  152.             self._headers.append((name, value))
  153.             return value
  154.         else:
  155.             return result
  156.  
  157.     
  158.     def add_header(self, _name, _value, **_params):
  159.         '''Extended header setting.
  160.  
  161.         _name is the header field to add.  keyword arguments can be used to set
  162.         additional parameters for the header field, with underscores converted
  163.         to dashes.  Normally the parameter will be added as key="value" unless
  164.         value is None, in which case only the key will be added.
  165.  
  166.         Example:
  167.  
  168.         h.add_header(\'content-disposition\', \'attachment\', filename=\'bud.gif\')
  169.  
  170.         Note that unlike the corresponding \'email.Message\' method, this does
  171.         *not* handle \'(charset, language, value)\' tuples: all values must be
  172.         strings or None.
  173.         '''
  174.         parts = []
  175.         if _value is not None:
  176.             parts.append(_value)
  177.         
  178.         for k, v in _params.items():
  179.             if v is None:
  180.                 parts.append(k.replace('_', '-'))
  181.                 continue
  182.             parts.append(_formatparam(k.replace('_', '-'), v))
  183.         
  184.         self._headers.append((_name, '; '.join(parts)))
  185.  
  186.  
  187.